package com.scopionstudio.djnonstop;

import java.util.ArrayList;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class ListMusicOffline extends Activity implements OnItemClickListener{
	private static final String TAG = ListMusicOffline.class.getSimpleName();
	private ImageButton ib_play,ib_next,ib_back;
	private ListView lst_music;
	private MediaPlayer mediaPlayer;
	private  String link="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_music_offline);
		initUI();
	}
	private void initUI(){
		mediaPlayer = new MediaPlayer();
		ib_play = (ImageButton) findViewById(R.id.imv_play_offline);
		ib_next = (ImageButton) findViewById(R.id.imv_next_offline);
		ib_back = (ImageButton) findViewById(R.id.imv_back_offline);
		ib_play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(link != ""){
				if(mediaPlayer.isPlaying()){
					mediaPlayer.pause();
					ib_play.setBackgroundResource(R.drawable.icon_play_offline);
				}else if(!mediaPlayer.isPlaying()){
					mediaPlayer.start();
					ib_play.setBackgroundResource(R.drawable.icon_pause_offline);
				}
				}else{
					Toast.makeText(ListMusicOffline.this, "Chọn bài hát để nghe", Toast.LENGTH_SHORT).show();
				}
			}
		});
		ib_next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		ib_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		lst_music = (ListView) findViewById(R.id.list_music_offline);
		lst_music.setOnItemClickListener(this);
	}
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
		// Set link to play
		playMusic(link);
	}
	private void playMusic(String linkPath){
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(linkPath);
			mediaPlayer.prepare();
			mediaPlayer.start();
		} catch (Exception e) {
			Log.e(TAG, "error :" + e.toString());
		}
	}
	private ArrayList<E> getAllSong(){
		
	}
	
}
